/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TCleanshop;
import com.gmail.blackdog1987.carcare.entity.TDriveschool;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderCarCleanRequestBuilder;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderDriveSchoolRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderCarCleanResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderDriveSchoolResponseHandler;

public class OrderDriveSchoolActivity extends BaseActivity implements OnResponseListener, OnClickListener {
	private Context mContext;

	CubeImageView head;
	TextView title;
	TextView location;
	TextView price;
	EditText name;
	EditText phone;
	EditText info;
	View pay;
	TDriveschool shop;

	OrderDriveSchoolRequestBuilder req = new OrderDriveSchoolRequestBuilder();
	OrderDriveSchoolResponseHandler handler = new OrderDriveSchoolResponseHandler();

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_driveschool);
		mContext = this;

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		head = (CubeImageView) findViewById(R.id.order_driveschool_head);
		title = (TextView) findViewById(R.id.order_driveschool_title);
		location = (TextView) findViewById(R.id.order_driveschool_location_str);
		// price = (TextView) findViewById(R.id.order_driveschool_cost);
		pay = findViewById(R.id.order_driveschool_pay);
		name = (EditText) findViewById(R.id.order_driveschool_name);
		phone = (EditText) findViewById(R.id.order_driveschool_phone);
		info = (EditText) findViewById(R.id.order_driveschool_content);
		price = (TextView) findViewById(R.id.order_driveschool_price);
	}

	public void reg() {
		shop = (TDriveschool) getIntent().getSerializableExtra("data");
		pay.setOnClickListener(this);
		handler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		findViewById(R.id.order_driveschool_location).setOnClickListener(this);
		findViewById(R.id.order_driveschool_call).setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		shop = (TDriveschool) getIntent().getSerializableExtra("data");
		ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
		if (shop.headurl != null && shop.headurl.length() != 0) {
			App.loadImg(head, imageLoader, shop.headurl);
		}
		title.setText(shop.getTitle());
		location.setText(shop.getLocation());
		price.setText(String.valueOf(shop.getPrice()));
		// price.setText(String.valueOf(shop.getPrice()));
	}

	private boolean validInput() {
		return !(name.getText().toString().trim().length() == 0 || phone.getText().toString().trim().length() == 0);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.order_driveschool_location:
			Intent intent = new Intent(OrderDriveSchoolActivity.this, MapGuidActivity.class);
			intent.putExtra("lat", shop.lat);
			intent.putExtra("lng", shop.lon);
			startActivity(intent);
			break;
		case R.id.order_driveschool_call:
			if (shop.phone == null || shop.phone.length() == 0) {
				Toast.makeText(mContext, "尚未录入商家电话", Toast.LENGTH_LONG).show();
				return;
			}
			Intent callintent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + shop.phone));
			startActivity(callintent);
			break;
		case R.id.back:
			finish();
			break;
		case R.id.order_driveschool_pay:
			if (!App.validUser(this) || !validInput())
				return;
			req.setSchoolid(String.valueOf(shop.getId())).setName(name.getText().toString()).setPhone(phone.getText().toString())
					.setInfo(info.getText().toString()).postJsonRequest(mContext, handler);
			break;
		default:
			break;
		}

	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// super.onActivityResult(requestCode, resultCode, data);
	// if (resultCode == Activity.RESULT_OK) {
	// switch (requestCode) {
	// case PayActivity.REQUESTCODE_PAY:
	// Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
	// finish();
	// break;
	// }
	// }
	// }

	@Override
	public void onResponse(IResponseHandler classz) {
		Toast.makeText(mContext, "下单成功", Toast.LENGTH_LONG).show();
		finish();
		// TProduct product = new TProduct();
		// product.body = "洗车";
		// product.price = Double.valueOf(shop.getPrice());
		// product.subject = shop.getTitle() + " 洗车 ";
		// product.table = "t_order_driveschool";
		// product.tableId = ((OrderCarCleanResponse)
		// classz.getResponse()).order.getId();
		// product.userid = new UserDao().getUser().id;
		// Intent i = new Intent(OrderDriveSchoolActivity.this,
		// PayActivity.class);
		// i.putExtra("data", product);
		// startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败", Toast.LENGTH_LONG).show();
	}
}
